package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.AdvertiseBo;
import com.cskaoyan.bean.bo.AdvertiseUpdateBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.vo.AdvertiseListVo;
import com.cskaoyan.bean.vo.AdvertiseUpdateVo;
import com.cskaoyan.bean.vo.AdvertiseVo;
import com.cskaoyan.service.AdvertiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/3 20:23
 */
@RestController // 该类下所有方法返回 json;
@RequestMapping("admin/ad")
public class AdvertiseController {
    @Autowired
    AdvertiseService advertiseService;

    /**
     * 业务层
     * GET /admin/ad/list?page=1&limit=20&sort=add_time&order=desc
     *
     * @param baseParam
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo listAll(BaseParam baseParam, String name, String content) {
        AdvertiseListVo advertiseList = advertiseService.listAll(baseParam,name,content);
        return BaseRespVo.ok(advertiseList);  // 使用 BaseRespVo 包裹advertiseList对象; status 默认值为 0;
    }

    /**
     * 业务层
     * POST /admin/ad/create
     * 必须加入注解;  POST+application/json;
     * @param advertiseBo
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create( @RequestBody  AdvertiseBo advertiseBo) {
        // System.out.println(advertiseBo);
        // 核心业务层-->调用核心逻辑层;
        AdvertiseVo advertiseVo = advertiseService.create(advertiseBo);
        return BaseRespVo.ok(advertiseVo);
    }

    /**
     *响应客户端 update 请求, 参数里携带了旧的 updateTime; 需要注意;
     * @return
     */
    @RequestMapping("update")
    public BaseRespVo update( @RequestBody AdvertiseUpdateBo advertiseUpdateBo) {
        System.out.println("--------"+advertiseUpdateBo);
        // 核心业务层-->调用核心逻辑层;/**/
        AdvertiseUpdateVo advertiseUpdateVo = advertiseService.updateOneAdvertise(advertiseUpdateBo);
        // 将advertiseUpdateVo内嵌于BaseRespVo里面进行返回;
        return BaseRespVo.ok(advertiseUpdateVo);
    }

    /**
     * 客户端传递信息, 相较于AdvertiseUpdateBo, 少了 link 和 deleted; 所以此处选择复用AdvertiseUpdateBo;
     * @param advertiseUpdateBo
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody AdvertiseUpdateBo advertiseUpdateBo){
      advertiseService.deleteOne(advertiseUpdateBo);
      // 使用 null ;因为客户端不需 data域;
        return BaseRespVo.ok(null);
    }

}
